<?php  
if ($_REQUEST['action'] == 'addproducts') {
    //  $file_name = $_REQUEST['file_name'];
    $post_type = $_REQUEST['post_type'] ? $_REQUEST['post_type'] : 'post';
    $producs = json_decode($_REQUEST['obj']);
    foreach ($producs as $item) {
        $category = explode('/', $item->catstrs);
        $catids = array();
        foreach ($category as $catname) {
            $catid = get_cat_id($catname);
            $catids[] = $catid;
        }
        $my_post = array(
            'post_title' => $item->title,
            'post_content' => $item->text,
            'post_type' => $post_type,
            'post_status' => 'publish',
            'post_author' => 34,
            'post_category' => $catids
        );
        $post_id = wp_insert_post($my_post);//插入了post
		
		
		/***********************添加meta************************/ 
		
		
        $tus = $tu = $product_gallery = array();
        $filename = '';
        $tus =  $item->tu;
  
        if (is_array($tus)) {			
            foreach ($tus as $tu) {
                $filename = $tu;
                $parent_post_id = $post_id;
		
              //  $filetype = wp_check_filetype(basename($filename) , null);//本地用报错
			// wp_check_filetype 判断文件类型 图片一般有 image/jpeg;image/png;
			 
					
					
                $wp_upload_dir = wp_upload_dir();
                $attachment = array(
                    'guid' => $wp_upload_dir['url'] . '/product_img/' . basename($filename) ,
                    'post_mime_type' =>'image/jpeg',// 图片类型 image/jpeg;image/png;
                    'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)) ,
                    'post_content' => '',
                    'post_status' => 'inherit'
                );

                $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
                $product_gallery[] = $attach_id;
            }
		
              add_post_meta($post_id, 'product_gallery', $product_gallery);
			 echo '['.$item->title.']上传成功<br>';
			 //print_r($filetype);exit();
        }
        // print_r($post_id);
        // exit();
        
    }
}
add_action('admin_menu', 'my_first_plugin');
function my_first_plugin() {
    add_menu_page('数据导入1', '批量上传产品(XuJoe)', 1, 'xujoe001', "init_xujoe");
    //  add_submenu_page('xujoe001', '数据导入', '数据导入', 7, 't9_eshopimport', 'data_import');
    if ($_GET['page'] == 'xujoe001') {
        outhtml();
    }; //
    
}
function init_xujoe() {
    echo "<script language='javascript'>

	function addtext(data) {
		$('#file').append(data);
		document.getElementById('update_tips').scrollTop = document.getElementById('update_tips').scrollHeight;
	}
	</script>";
    global $wpdb;
    $displaymain = true;
}
?>
<?php
function phpout($var) {
    echo '<script>console.log("php输出：");console.log(' . $var . ');</script>';
}
 

function outhtml() {
    echo '
    

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>批量上传产品</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="js/jquery-3.3.1.min.js"></script>  
</head>

<body>
    <div>批量添加产品(格式：[{"catstrs":"Products/2级/3级","title":"<a style="color:red;">产品名称</a>","tu":["<a style="color:red;">图1.jpg</a>","<a style="color:red;">图二.jpg</a>"],"text":"<a style="color:red;">产品描述</a>"}])</div>
<!-- ["Products","<a style="color:red;">2级</a>","<a style="color:red;">3级</a>"]  <a style="color:red;">tu1.jpg|tu2.jpg</a> -->
    <style>
        .div-a {
            float: left;
            width: 70%;
            /* border: 1px solid #F00 */
        }

        .div-b {
            float: left;
            width: 28%;
            /* border: 1px solid #000 */
        }
    </style>
 <form action="" method="post">
    <div class="div-a"> <textarea id="productsobj" name="obj" style="margin: 0.989583px; width: 99%; height: 300px;" placeholder="这里放产品JSON,必须先创建了对应的分类再上传产品，不然会报错"></textarea></div>
    <div class="div-b"> <textarea id="productsshow" style="margin: 0.989583px; width: 99%; height: 300px;" placeholder="显示结果："></textarea></div> 
    <br> </button> <input type="submit" value="添加产品" />   
  <p> 
  <p><input type="hidden" name="action" value="addproducts" /></p>
 
</form>
    </div>


  
    <br /><br /><br />
    <div>批量添加产品分类</div>
    <textarea id="cats" style="margin: 0.989583px; width: 95%; height: 300px;" placeholder="Products/小米/小米/小米8se/"></textarea><br><button id="addcats" class="button button-primary">添加产品分类</button>


</body>
  
    <script src="js/products.js"></script>
    <script src="js/addcat.js"></script>

</html> 
    ';
}
?> 
